<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容 -->
      <el-card>
        <el-menu default-active="1" class="el-menu-demo" mode="horizontal">
          <el-menu-item index="1">处理中心</el-menu-item>
        </el-menu>
        <div class="tree-card">
          <!-- 用了一个行列布局 -->
          <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts"></tree-tools>
          <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
            <tree-tools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts"></tree-tools>
          </el-tree>
        </div>
      </el-card>
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="currentNode" @addDepts="getDepartments"></add-dept>
    </div>
  </div>
</template>

<script>
import TreeTools from './components/tree-tool.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments, getDeptsDetailsById } from '@/api/departments'
import { transListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      company: {},
      departs: [],
      defaultProps: {
        label: 'name'
      },
      showDialog: false,
      currentNode: null,
      loading: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取部门
    async getDepartments() {
      // 给获取数据添加加载进度条
      this.loading = true
      const results = await getDepartments()
      // console.log(results)
      this.company = { name: results.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(results.depts, '')
      this.loading = false
    },
    addDepts(node) {
      // 新增部门弹出层显示
      this.showDialog = true
      // 将点击的树节点所有值 赋值给 currentNode
      this.currentNode = node
    },
    editDepts(node) {
      this.showDialog = true
      this.currentNode = node
      // 不能直接调用getDeptsDetailsById方法，因为获取详细信息是子组件的方法
      // 我们需要在这个位置 调用子组件的方法
      // 父组件 调用子组件的方法
      // addDept为子组件AddDept的ref属性值
      this.$refs.addDept.getDeptsDetailsById(node.id) // 直接调用子组件中的方法 传入一个id
    }
  }
}
</script>

<style>
.el-icon-office-building {
  font-weight: bolder;
  margin-right: 4px;
}
.el-menu--horizontal > .el-menu-item.is-active {
  color: #409eff;
}
.el-menu--horizontal .el-menu-item:not(.is-disabled):focus,
.el-menu--horizontal .el-menu-item:not(.is-disabled):hover {
  color: #409eff;
  cursor: default;
}
.tree-card {
  margin: 30px 140px;
  padding-left: 10px;
  font-size: 14px;
  border-bottom: 1px solid #ebeef5;
}
</style>
